package Servlet;

import DAO.*;

import javabean.Admin;
import javabean.Customer;
import javabean.Staff;
import javabean.Technician;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String role = req.getParameter("role");
        System.out.println(role);
        HttpSession session = req.getSession();
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        boolean isLogin = true;
        switch (role) {
            case "Customer":
                CustomerDAO customerDAO = (CustomerDAO) DAOFactory.getCustomerDAOInstance();
                Customer customer = new Customer();
                customer.setCustomerID(username);
                customer.setPassword(password);
                try {
                    if (customerDAO.login(customer)) {
                        Customer loginCustomer = customerDAO.searchCustomer(username);
                        session.setAttribute("loginCustomer", loginCustomer);
                        session.setAttribute("isLogin", isLogin);
                        out.print("<script>alert('登录成功!');window.location.href='Personal_Info.jsp'</script>");
                    } else {
                        isLogin = false;
                        session.setAttribute("isLogin", isLogin);
                        out.print("<script>alert('账号或密码错误!');window.location.href='Login.jsp'</script>");
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                break;
            case "admin": {
                AdminDAO adminDAO = (AdminDAO) DAOFactory.getAdminDAOInstance();
                Admin admin = new Admin();
                admin.setAdminID(username);
                admin.setPassword(password);
                System.out.println(admin.getAdminID() + " " + admin.getPassword());
                try {
                    if (adminDAO.login(admin)) {
                        admin = adminDAO.getAdmin(username);
                        session.setAttribute("loginAdmin", admin);
                        session.setAttribute("isLogin", isLogin);
                        out.print("<script>alert('登录成功!');window.location.href='Admin.jsp'</script>");
                    } else {
                        System.out.println("Login failed!");
                        isLogin = false;
                        session.setAttribute("isLogin", isLogin);
                        out.print("<script>alert('账号或密码错误!');window.location.href='Login.jsp'</script>");
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

                break;
            }
            case "technician": {
                TechnicianDAO technicianDAO = (TechnicianDAO) DAOFactory.getTechnicianDAOInstance();
                Technician technician = new Technician();
                technician.setTechnicianID(username);
                technician.setPassword(password);
                try {
                    if (technicianDAO.login(technician)) {
                        System.out.println("Login successfully!");
                        technician = technicianDAO.getTechnicianByID(username);
                        session.setAttribute("loginTechnician", technician);
                        session.setAttribute("isLogin", isLogin);
                        out.print("<script>alert('登录成功!');window.location.href='Technician.jsp'</script>");
                    } else {
                        System.out.println("Login failed!");
                        isLogin = false;
                        session.setAttribute("isLogin", isLogin);
                        out.print("<script>alert('账号或密码错误!');window.location.href='Login.jsp'</script>");
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                break;
            }
            case "staff": {
                StaffDAO staffDAO = (StaffDAO) DAOFactory.getStaffDAOInstance();
                Staff staff = new Staff();
                staff.setStaffID(username);
                staff.setPassword(password);
                try {
                    if (staffDAO.Login(staff)) {
                        System.out.println("Login successfully!");
                        staff = staffDAO.getStaff(username);
                        session.setAttribute("loginStaff", staff);
                        session.setAttribute("isLogin", isLogin);
                        out.print("<script>alert('登录成功!');window.location.href='Staff.jsp'</script>");
                    } else {
                        System.out.println("Login failed!");
                        isLogin = false;
                        session.setAttribute("isLogin", isLogin);
                        out.print("<script>alert('账号或密码错误!');window.location.href='Login.jsp'</script>");
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                break;
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
